
package com.example.onlineshop.Activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.onlineshop.Adapter.BestDealAdapter;
import com.example.onlineshop.Adapter.CategoryAdapter;
import com.example.onlineshop.Domain.CartEntity;
import com.example.onlineshop.Domain.CategoryDomain;
import com.example.onlineshop.Domain.ItemsDomain;
import com.example.onlineshop.Helper.CartsHelper;
import com.example.onlineshop.R;
import com.example.onlineshop.db.SqliteCarOpenHelper;
import com.example.onlineshop.db.SqliteShopOpenHelper;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private RecyclerView.Adapter catAdapter, bestDealAdapter;
    private RecyclerView recyclerViewCat, recyclerViewBestDeal;

    private ImageView cartImg,imageView6;
    private LinearLayout lin_cart;
    private EditText editTextText;//商品输入框
    private  String shopName="";//商品名称
    SqliteShopOpenHelper dbHelper;
    SQLiteDatabase db;
    List<CartEntity> list;
    TextView textView11;
    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);



        cartImg = findViewById(R.id.cartImg);
        lin_cart = findViewById(R.id.lin_cart);
        imageView6 = findViewById(R.id.imageView6);
        editTextText = findViewById(R.id.editTextText);
        recyclerViewBestDeal = findViewById(R.id.bestView);
        textView11=findViewById(R.id.textView11);
        recyclerViewBestDeal.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));
        //初始化创建数据库
        dbHelper = new SqliteShopOpenHelper(this, "cart.db", null, 1);
        db = dbHelper.getWritableDatabase();
        list=new ArrayList<>();
         list=dbHelper.QueryAll();
        //如果数据库没有商品 把商品信息添加到数据库
        if (list.size()>0) {
            Log.e("删除","有数据先删除数据库数据");
            dbHelper.deleteAll();
        }
        InsertDabta();
        lin_cart.setOnClickListener(v -> startActivity(
                new Intent(MainActivity.this, CartsHelper.class))

        );
        // 搜索商品
        imageView6.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                shopName=editTextText.getText().toString().trim();//获取输入的商品名称并去除空格
                //判断商品名称是否为空
                if (TextUtils.isEmpty(shopName) ){
                    Toast.makeText(MainActivity.this,"请输入商品名称",Toast.LENGTH_SHORT).show();
                }else{
                    list=dbHelper.QueryByName(shopName);
                    initRecyclerViewBestDeal(list);

                }
            }
        });
        //查詢全部商品
        textView11.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                list.clear();
              list=  dbHelper.QueryAll();
                bestDealAdapter.notifyDataSetChanged();
            }
        });
        initRecyclerviewCat();
        initLocation();
        initRecyclerViewBestDeal(list);
    }

    private void initLocation() {
        String[] items = new String[]{"阜阳, 安徽", "合肥, 安徽", "亳州, 安徽", "杭州, 浙江", "宁波, 浙江"};
        final Spinner locationSpin = findViewById(R.id.spinner2);
        ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, items);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        locationSpin.setAdapter(adapter);
    }

    private void initRecyclerviewCat() {
        ArrayList<CategoryDomain> items = new ArrayList<>();
        items.add(new CategoryDomain("cat2", "蔬菜"));
        items.add(new CategoryDomain("cat1", "水果"));
        items.add(new CategoryDomain("cat3", "奶制品"));
        items.add(new CategoryDomain("cat4", "饮料"));
        items.add(new CategoryDomain("cat5", "粮食"));
        items.add(new CategoryDomain("cat6", "零食"));


        recyclerViewCat = findViewById(R.id.catView);
        recyclerViewCat.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));

        catAdapter = new CategoryAdapter(items);
        recyclerViewCat.setAdapter(catAdapter);

    }

    private void initRecyclerViewBestDeal(List<CartEntity> mlist) {
        bestDealAdapter = new BestDealAdapter(mlist);
        recyclerViewBestDeal.setAdapter(bestDealAdapter);
    }

    public void InsertDabta() {
        ArrayList<ItemsDomain> items = new ArrayList<>();
        items.add(new ItemsDomain("apple", "优选红富士", 8.8, "红富士苹果，皮薄肉厚，色泽鲜艳如玛瑙。口感脆嫩多汁，甜度适中，风味独特。果实个大均匀，富含多种维生素和矿物质。自日本引进后，深受全球消费者喜爱，是健康美味的水果代表。", 4));
        items.add(new ItemsDomain("orange", "鲜橙果切", 8.9, "鲜橙，色泽金黄，皮薄易剥。肉质饱满多汁，甜中带酸，清新爽口。富含维生素C和纤维素，营养丰富。可直接食用，亦可榨汁制饮。全球种植广泛，是人们日常喜爱的健康水果。", 4.2));
        items.add(new ItemsDomain("watermelon", "皇冠麒麟西瓜", 40, "皇冠麒麟瓜，外形独特，纹路清晰似古代冠冕。果肉脆甜多汁，清香味美，口感极佳。含糖量高，营养丰富，深受食客青睐。是夏季消暑佳品，被誉为瓜中极品。", 4.9));
        items.add(new ItemsDomain("strawberry", "红艳/红颜草莓", 26.9, "红艳/红颜草莓，果实饱满，色泽艳丽如宝石。口感细腻柔软，酸甜适中，香气浓郁。营养丰富，含有大量维生素C和抗氧化物。是春日里的美味佳果，深受人们喜爱。", 4.1));
        items.add(new ItemsDomain("mango", "水仙芒果", 19.9, "水仙芒果，果实椭圆，皮薄光滑。果肉橙黄细腻，甜度高，汁多味鲜。香气浓郁，营养丰富。是热带水果中的上品，深受消费者喜爱。", 4.5));
        items.add(new ItemsDomain("pitaya", "蜜宝红心火龙果", 18.8, "蜜宝红心火龙果，外表鲜艳诱人，内含丰富营养。果肉如玫瑰般绽放，口感细腻多汁，甜中带微酸，清新宜人。富含维C与膳食纤维，健康美味兼具。夏日解暑，冬日养颜，四季皆宜的水果佳品。", 4.3));
        items.add(new ItemsDomain("pomegranate", "突尼斯软籽石榴", 30, "突尼斯软籽石榴，果皮薄而易剥，显露出饱满的红宝石般籽粒。汁液丰富、甜度高，籽软可食，无需繁琐去籽。营养丰富，含有维生素C和抗氧化物，健康美味。适合直接食用或制成果酱，是老少皆宜的健康水果。", 4.4));
        items.add(new ItemsDomain("kiwifruit", "脆香猕猴桃", 8.9, "脆香猕猴桃，外皮薄而易剥，内藏翠绿果肉，清新爽口。肉质细腻且多汁，甜度适中带微酸，后味留有淡淡的果香。富含维生素C及膳食纤维，促进消化，增强免疫。直接食用或搭配料理皆宜，是健康饮食的优选水果。", 4.4));
        items.add(new ItemsDomain("hamimelon", "千觅网纹瓜", 16.9, "千觅网纹瓜，外表翠绿带细腻纹理，内含甘甜多汁果肉。口感脆嫩，甜而不腻，带有淡淡芳香。营养丰富，含多种维生素和矿物质，健康美味。夏日消暑佳品，可切片食用或制成果盘，清凉解渴，增添生活甜蜜滋味。", 4.4));
        items.add(new ItemsDomain("mangosteen", "进口山竹", 14.9, "进口山竹，表皮硬厚，内藏丰润甘甜的果肉。口感绵密滑顺，甜度浓郁，带有特有的香气。富含多种维生素及矿物质，营养价值高。适合直接食用，亦可制成果酱或冰淇淋，是热带水果中的极品，深受大众喜爱。", 4.4));
        items.add(new ItemsDomain("avocado", "秘鲁即食牛油果", 15.9, "秘鲁即食牛油果，果实肥厚圆润，切开后呈现出鲜嫩的绿色果肉。口感细腻似奶油，入口即化，味道清新宜人。富含健康脂肪和多种维生素，营养价值高，有助于降低胆固醇。适合制作沙拉或寿司，是健康美食爱好者的首选食材。", 4.4));
        items.add(new ItemsDomain("passionfruit", "紫金百香果", 9.9, "紫金百香果，表皮金紫相间，内含饱满的酸甜种子。汁液丰富，香气浓郁扑鼻，是调味和饮品的佳选。富含维生素及抗氧化物，助力美容养颜。直接食用或搭配美食，提升风味与健康，深受健康生活追求者喜爱。", 4.4));
        items.add(new ItemsDomain("passionfruit", "榴莲", 9.9, "紫金百香果，表皮金紫相间，内含饱满的酸甜种子。汁液丰富，香气浓郁扑鼻，是调味和饮品的佳选。富含维生素及抗氧化物，助力美容养颜。直接食用或搭配美食，提升风味与健康，深受健康生活追求者喜爱。", 4.4));

         //插入數據
         for (int i = 0; i <items.size() ; i++) {
            ItemsDomain item=items.get(i);
            dbHelper.insertData(item.getTitle(),item.getImgPath(),item.getPrice()+"",1,item.getRate()+"",item.getDescription());
        }
         //查詢全部數據
        list=dbHelper.QueryAll();
//
    }
}
